extends NodeState



@export var player: Player

var velocity: Vector2 = Vector2.ZERO
var height: float = 0.0
var height_speed: float = 0.0


func _on_process(delta : float) -> void:
	if _changing_state:
		return
	player.position += velocity * delta
	process_animation()
	process_height(delta)


func _on_enter() -> void:
	velocity = Vector2.ZERO
	height = player.spawn_drop_height
	set_disabled(true)
	player.look_at_direction(Vector2.RIGHT)


func _on_exit() -> void:
	set_disabled(false)



func process_animation():
	player.animationPlayer.play(Player.ANIMATION_DROP)


func process_height(delta: float):
	player.characterSprite.position = Vector2.UP * height

	height += height_speed * delta
	# 高度降为0时进入倒地状态
	if height < 0.0:
		height = 0.0
		velocity = Vector2.ZERO
		player.characterSprite.position = Vector2.ZERO
		transition_state(Player.STATE_LAND)
	else:
		height_speed -= player.gravity * delta


func set_disabled(disabled: bool):
	player.collisionShape.set_deferred("disabled", disabled)
	player.damageEmitter.set_deferred("monitoring", not disabled)
	player.damageReceiver.set_deferred("monitorable", not disabled)
	player.collectibleSensor.set_deferred("monitoring", not disabled)
	player.projectileAim.set_deferred("enabled", not disabled)
